package com.quick.order.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quick.common.util.PageUtil;
import com.quick.common.Exception.exception.BaseException;
import com.quick.common.bean.Driver;
import com.quick.common.bean.Order;
import com.quick.common.bean.Payment;
import com.quick.common.dto.OrderPageDTO;
import com.quick.common.service.*;
import com.quick.common.util.Result;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@GlobalTransactional
@RequestMapping("/order")
public class OrderController {
    @DubboReference
    IDriverService driverService;
    @DubboReference
    IOrderService orderService;
    @DubboReference
    IUserService userService;
    @DubboReference
    IMQService mqService;
    @DubboReference
    IPaymentService paymentService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    @Qualifier("orderServiceImpl")
    IOrderService OrderService;
    @PostMapping("/addOrder")
    public Result<String> order(@RequestBody  Order  order) {

        try {
            order.setOrder_no("AXI"+UUID.randomUUID()+order.getUser_id());
            order.setOrder_time(LocalDateTime.now());
            order.setStatus(0);
            String order1 = JSON.toJSONString(order);
            mqService.sendMessage("call_car.order", order1);
        } catch (Exception e) {
            throw new BaseException(400,"创建订单失败");
        }
        return Result.success("正在寻找附近空闲车辆·········");
    }
    @RequestMapping("/getDriver")
    public Driver getDriver() {
        return driverService.getDriverById(1);
    }

    @GetMapping("/getOrder/{id}")
    public Result<Order> getOrder(@PathVariable("id") Integer user_id) {
        String orderId = redisTemplate.opsForValue().get("user:" + user_id + "order:");
        if (orderId == null) {
            return Result.error("用户未下单");
        }
        Order order = orderService.getOrderById(Integer.parseInt(orderId));
        return Result.success(order);
    }

    @PutMapping("/editStatus")
    public Result<String> editStatus(@RequestParam("id") Integer id,@RequestParam("status") Byte status) {
        orderService.editStatus(id,status);
        return Result.success("订单状态发生改变");
    }
    @PutMapping("/editOrder")
    public Result<String> editOrder(@RequestBody Order order) {
        System.out.println("修改订单信息"+ order);
        orderService.updateOrder(order);
        if(order.getStatus() != null && order.getStatus()==3){
            redisTemplate.delete("user:"+order.getUser_id()+"order:");
        }
        return Result.success("订单信息发生改变");
    }
    @PostMapping("/addPayment")
    public Result<Integer> addPayment(@RequestBody Payment payment) {
        int i = paymentService.addPayment(payment);
        return Result.success(i);
    }
    @GetMapping("/getOrderList")
    public Result<IPage<Order>> getOrderList(// 分页参数（必传，默认值兜底）
                                             Integer current,Integer size,
                                             // 查询条件（可选，用包装类接收 null）
                                             Order order) {
        IPage< Order> page = new Page<>(current,size);
        System.out.println("page-----------------------"+ page +"order----------"+ order);
        IPage<Order> page1 = OrderService.getPage(order,page);
        System.out.println("page1-----------------------"+ page1);
        return Result.success(page1);
    }
    @GetMapping("/getOrderList1")
    public Result<IPage<Order>> getOrderList1(// 分页参数（必传，默认值兜底）
                                             Integer current,Integer size,
                                             // 查询条件（可选，用包装类接收 null）
                                             Order order) {
        IPage< Order> page = new Page<>(current,size);
        System.out.println("page-----------------------"+ page +"order----------"+ order);
        IPage<Order> page1 = OrderService.getPage1(order,page);
        System.out.println("page1-----------------------"+ page1);
        return Result.success(page1);
    }

    @GetMapping("/getPayment/{id}")
    public Result<Payment> getPayment(@PathVariable("id") Integer id) {
        Payment payment = paymentService.getPaymentById(id);
        return Result.success(payment);
    }
}
